﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataBase  mydatabase = new DataBase();
        public DataSet ds = new DataSet();
        public DataTable dt = new DataTable();
        public void updatdata()
        {
            string sql = "select * from student";
            if (mydatabase.doupdate(ds, sql, dt.TableName))
            {
                MessageBox.Show("数据更新成功", "提示", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("数据更新失败,请检查数据是否合法", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ds = mydatabase.getdataset("select * from student");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt.DefaultView;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            updatdata();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("数据删除后将不可恢复，真的要删除吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                dataGridView1.Refresh();
                updatdata();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            dataGridView1.Rows[i].Selected = false;
            dataGridView1.Rows[0].Selected = true;
            showmess();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            if (i < dataGridView1.Rows.Count - 1)
            {
                dataGridView1.Rows[i].Selected = false;
                dataGridView1.Rows[i + 1].Selected = true;
                showmess();
                {
                    MessageBox.Show("已经是最后一项", "提示", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            if (i != 0)
            {
                dataGridView1.Rows[i].Selected = false;
                dataGridView1.Rows[i - 1].Selected = true;
                showmess();
            }
            else
            {
                MessageBox.Show("已经是第一项", "提示", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedRows[0].Index;
            dataGridView1.Rows[i].Selected = false;
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            showmess();
        }
        private void showmess()
        {
            toolStripStatusLabel1.Text = string.Format("当前是第{0}条记录，共{1}条记录", dataGridView1.SelectedRows[0].Index + 1, dataGridView1.Rows.Count);
            toolStripProgressBar1.Value = (int)((double)(dataGridView1.SelectedRows[0].Index) / dataGridView1.Rows.Count * 100);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}
